<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.awt.Color"%>
<%@ page import = "com.wsu.basic.imageio.ImageTools"%>
<%@ page
	import="org.apache.commons.fileupload.DiskFileUpload,java.text.SimpleDateFormat,org.apache.commons.fileupload.FileItem,java.io.File"%>
<%@ page
	import="org.apache.commons.fileupload.FileUploadException,org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>

<%
int useid= new Random().nextInt(233);

String c_pname = "";
String spath = "";

final int MAX_SIZE = 300 * 1024 * 1024;// 设置上传文件最大为 300k
final int ONE_MAX_SIZE = 100 * 1024 * 1024;
// 允许上传的文件格式的列表 
final String[] allpict = new String[] { ".jpg", ".jpeg",
		".gif", ".flv", ".png", ".bmp", ".JPG", ".FLV",
		".GIF", ".JPEG", ".PNG", ".BMP" };

String path2 = request.getRealPath("/pic");
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸，这里是4MB
//        fu.setSizeMax(41943040);
fu.setSizeMax(MAX_SIZE);
//     System.out.println("fu="+fu);
// 设置缓冲区大小，这里是4kb
fu.setSizeThreshold(MAX_SIZE);
// 设置临时目录：
fu.setRepositoryPath(path2 + "\\temp");
// 得到所有的文件：
List fileItems;
String message = "";
//生成当前时间 的
SimpleDateFormat formater = new SimpleDateFormat(
		"yyyyMMddHHmmssS");
String tday = formater.format(new Date());
//System.out.println("useid="+useid);
useid = useid + Integer.parseInt(tday.substring(8, 16));
tday = tday.substring(0, 8) + useid ;

//System.out.println("tday="+tday);
int it = 1;
List<String> ls = new ArrayList<String>();
Boolean namenull = false;

try {
	fileItems = fu.parseRequest(request);

	Iterator iter = fileItems.iterator();
	while (iter.hasNext()) {
		FileItem item = (FileItem) iter.next();

		if (!item.isFormField()) {
			String name = item.getName();
			long size = item.getSize();
			if ((name == null || name.equals("")) && size == 0) {
				//	 message="succ";
				namenull = true;
				continue;
			}

			String tnam = name.substring(name.lastIndexOf("."),
					name.length());
			boolean picge = false;
			for (String sin : allpict) {
				if (tnam.equals(sin)) {
					picge = true;
				}
			}
			if (!picge) {
				out.println("上传有误");
				//System.out.println("1");
			} else {
				if (size > ONE_MAX_SIZE) {
					//System.out.println("2");
					out.println("大小不能超过30m");
					return;
				}

				it++;
				//返回文件名，以时间为名，路径：e:/temp
				String path = path2 + "\\"
						+ tday.substring(0, 6) + "\\"
						+ tday.substring(6, 8) + "\\" + tday
						+ it + tnam;
				String savpath = path2 + "\\"
						+ tday.substring(0, 6) + "\\"
						+ tday.substring(6, 8) + "\\thumb_"
						+ tday + it + tnam;
				
				//获得图片名
				c_pname = tday + it + tnam;
				
				File file = new File(path);

				spath =  "/pic/"
				+ tday.substring(0, 6) + "/"
				+ tday.substring(6, 8) + "/thumb_" + tday
				+ it + tnam;
				//	 		ls.add(tday+it+tnam);
				//	 		System.out.println(file.getParentFile());
				boolean b = file.getParentFile().isDirectory();
				if (!b) {
					file.getParentFile().mkdirs();//创建目录
				}

				try {
					//写入图片
					item.write(file);
					
					//加入水印
					//ImageTools.pressText("惠多网 138do.com", path, path,"宋体", 36, Color.white, 16, 0, 0, 0.6f);
					
					//缩略图
					float p = 170;
					//System.out.println(savpath);
					//ImageTools.toZipJpg(path,savpath,p);
					//ImageThumb.toZipJpg(path,savpath,170);
					//加水印
					//ImageTools.pressImgText("惠多网 ", savpath, savpath);

				} catch (Exception e) {
					System.out.println(e.getMessage());
					out.println("上传有误");
				}
			}
		}

	}
	
	out.print("successed:"+c_pname+"#"+spath);
} catch (FileUploadException e) {
	if (e instanceof SizeLimitExceededException) {
		System.out.println("上传文件有误upload.jsp"); 
		out.println("上传有误,请检查大小");

	}
}

	//kissy editor 因为用的是ajax上传,此处输出格式
	// 成功时，返回 ["0", "图片地址"]
	// 失败时，返回 ["1", "错误信息"]
	//System.out.println("[\""+a+"\", \"" + spath + "\","+c_pname+"]");	

	//out.println("[\"0\", \"" + spath + "\"]");

	//System.out.println(spath);
	
%>



